﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChartDirector;

namespace GoodTrader.GTDownload
{
    public class CpFutureData : CpData
    {
        ChartDirector.WinChartViewer chartCtrl = new ChartDirector.WinChartViewer();

        //5봉 평균으로 사용
        public double[] MA = new double[GTGlobal.MaxDownloadCount];
        public double[] MA2 = new double[GTGlobal.MaxDownloadCount];
        public double[] MA3 = new double[GTGlobal.MaxDownloadCount];

        // 20봉 평균으로 사용
        public double[] MA4 = new double[GTGlobal.MaxDownloadCount];

        // 고정인덱스 - 프로그램 처음 시작시 고정되며 프로그램이 진행됨에 따라 갱신된다.
        // 이 인덱스는 하나의 봉에 대해서 하나만 가지며 그 봉에 대하여 인덱스는 절대로 바뀌지 않는다.
        public double[] ConstIndex = new double[GTGlobal.MaxDownloadCount];

        public double[] DayIndex = new double[GTGlobal.MaxDownloadCount];

        public double[] High = new double[GTGlobal.MaxDownloadCount];
        public double[] Low = new double[GTGlobal.MaxDownloadCount];
        public double[] Open = new double[GTGlobal.MaxDownloadCount];
        public double[] Close = new double[GTGlobal.MaxDownloadCount];
        // 증권사에서 받아온 원래 상태의 시간. 형식은 YYYYMMDD 로 부동소수. 예) 20140309.
        public double[] Time = new double[GTGlobal.MaxDownloadCount];
        public double[] Date = new double[GTGlobal.MaxDownloadCount];
        public double[] Amount = new double[GTGlobal.MaxDownloadCount];
        public double[] UnSet = new double[GTGlobal.MaxDownloadCount];
        public double[] Volume = new double[GTGlobal.MaxDownloadCount];
        public double[] Basis = new double[GTGlobal.MaxDownloadCount];
        public DateTime[] DT = new DateTime[GTGlobal.MaxDownloadCount];

        // 고점과 저점을 위한 데이터
        public double[] DH05 = new double[GTGlobal.MaxDownloadCount];
        public double[] DH75 = new double[GTGlobal.MaxDownloadCount];
        public double[] DH12 = new double[GTGlobal.MaxDownloadCount];
        public double[] DH = new double[GTGlobal.MaxDownloadCount];

        public double[] DL05 = new double[GTGlobal.MaxDownloadCount];
        public double[] DL75 = new double[GTGlobal.MaxDownloadCount];
        public double[] DL12 = new double[GTGlobal.MaxDownloadCount];
        public double[] DL = new double[GTGlobal.MaxDownloadCount];

        /*
         * @brief : 이 함수는 주어진 a_ConstIndex와 일치하는 인덱스를 반환한다. 
         */ 
        public int FindIndexByConstIndex(int a_ConstIndex)
        {
            for(int i = ConstIndex.Length - 1; i >= 0; i--)
            {
                int curConstIndex = (int)ConstIndex[i];
                if (curConstIndex == a_ConstIndex)
                {
                    return i;
                }
            }

            return -1;
        }

        public void RecalConstIndex()
        {
            double sIndex = ConstIndex[0];
            Console.WriteLine("const StartIndex" + sIndex.ToString());
            for (int i = 1; i < ConstIndex.Length; i++)
            {
                ConstIndex[i] = sIndex + i; 
            }
        }

        public void RecalDayIndex()
        {
            double oldDate = Date[0];
            int k = 0;
            for(int i = 0; i < Date.Length; i++)
            {
                double curDate = Date[i];
                if (curDate != oldDate)
                    k = 0;
                
                DayIndex[i] = k;
                k++;
                oldDate = curDate;
            }
        }

        public void DayHighLow()
        {
            double oldDate = Date[0];
            double dayHigh = High[0];
            double dayLow = Low[0];
            for (int i = 0; i < Date.Length; i++)
            {
                double curDate = Date[i];
                double curHigh = High[i];
                double curLow = Low[i];
                if (curDate != oldDate)
                {
                    dayHigh = High[i];
                    dayLow = Low[i];
                }

                if (curHigh > dayHigh)
                    dayHigh = curHigh;
                if (curLow < dayLow)
                    dayLow = curLow;

                DH[i] = dayHigh;
                DL[i] = dayLow;
                DH05[i] = dayHigh - 0.5;
                DH75[i] = dayHigh - 0.75;
                DH12[i] = dayHigh - 1.2;

                DL05[i] = dayLow + 0.5;
                DL75[i] = dayLow + 0.75;
                DL12[i] = dayLow + 1.2;

                oldDate = curDate;
            }
        }

        public int FindIndex(double aDate, double aTime)
        {
            for(int i = 0; i < Date.Length; i++)
            {
                if (Date[i] == aDate && Time[i] == aTime)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}
